package cn.rmc.hrm;

import cn.rmc.hrm.constants.BaseConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import to.CourseStationMessageTo;
import to.EmailTo;
import to.SmsTo;

import java.io.IOException;


@Component
@Slf4j
public class Consumer {
    /**
     * 消费短信
     */
    @RabbitListener(queues = BaseConstant.CourseConstant.QUEUE_COURSE_SMS)
    public void ConsumptionSms(@Payload SmsTo smsTo, Message message, Channel channel){
        log.info("发送短信 {}",smsTo);
//        手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息：{}，失败！", smsTo);
        }
    }
    /**
     * 消费邮件
     */
    @RabbitListener(queues = BaseConstant.CourseConstant.QUEUE_COURSE_EMAIL)
    public void ConsumptionEmial(@Payload EmailTo emailTo, Message message, Channel channel){
        log.info("发送邮件 {}",emailTo);
//        手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息：{}，失败！", emailTo);
        }
    }
    /**
     * 消费站内信
     */
    @RabbitListener(queues = BaseConstant.CourseConstant.QUEUE_COURSE_STATION_MESSAGE)
    public void ConsumptionSM(@Payload CourseStationMessageTo courseStationMessageTo, Message message, Channel channel){
        log.info("发送站内信 {}",courseStationMessageTo);
//        手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("消费消息：{}，失败！", courseStationMessageTo);
        }
    }
}
